#include "StdAfx.h"
#include "WidgetTagGroup.h"
#include "RawTag.h"

typedef std::map<CEGUI::Window*,CEGUIPlus::WidgetTagGroup*> WindowTagGroupMap;
WindowTagGroupMap windowAndTagGroup;


CEGUIPlus::WidgetTagGroup::WidgetTagGroup( CEGUI::Window* tagTarget ):
target(tagTarget)
{
// 	void* userdata = tagTarget->getUserData();
// 	if(userdata!=0)
// 	{
// 		RawTag::Ptr rawTag = RawTag::Ptr(new RawTag(userdata));
// 		tags.push_back(rawTag);
// 	}
}

CEGUIPlus::WidgetTagGroup::~WidgetTagGroup()
{
	tags.clear();	
}

WidgetTagGroup::TagList& CEGUIPlus::WidgetTagGroup::GetTagList()
{
	return tags;
}

WidgetTagGroup* CEGUIPlus::WidgetTagGroup::GetTagsOf( CEGUI::Window* targetWindow )
{	
	WindowTagGroupMap::iterator i = windowAndTagGroup.find(targetWindow);
	if(i==windowAndTagGroup.end())
	{
		WidgetTagGroup* ret = new WidgetTagGroup(targetWindow);
		windowAndTagGroup[targetWindow]=ret;
		return ret;
	}
	else
		return i->second;
}

bool CEGUIPlus::WidgetTagGroup::HaveTags( CEGUI::Window* targetWindow )
{
	return windowAndTagGroup.find(targetWindow)!=windowAndTagGroup.end();
}

void CEGUIPlus::WidgetTagGroup::ClearTags( CEGUI::Window* targetWindow )
{
	WindowTagGroupMap::iterator i = windowAndTagGroup.find(targetWindow);
	if(i!=windowAndTagGroup.end())
	{		
		windowAndTagGroup.erase(i);
		delete i->second;
	}
}